namespace TerritoryGame.Domain.ValueObjects;

/// <summary>
/// 位置值对象 - 表示画布上的坐标
/// </summary>
public record Position(int X, int Y)
{
    public static Position Origin => new(0, 0);
    
    /// <summary>
    /// 计算到另一个位置的距离
    /// </summary>
    public double DistanceTo(Position other)
    {
        return Math.Sqrt(Math.Pow(X - other.X, 2) + Math.Pow(Y - other.Y, 2));
    }
    
    /// <summary>
    /// 检查位置是否在有效范围内
    /// </summary>
    public bool IsValid(int maxWidth = 800, int maxHeight = 600)
    {
        return X >= 0 && X < maxWidth && Y >= 0 && Y < maxHeight;
    }
    
    /// <summary>
    /// 获取相邻位置
    /// </summary>
    public IEnumerable<Position> GetAdjacentPositions()
    {
        var directions = new[]
        {
            (-1, 0), (1, 0), (0, -1), (0, 1),
            (-1, -1), (-1, 1), (1, -1), (1, 1)
        };
        
        foreach (var (dx, dy) in directions)
        {
            yield return new Position(X + dx, Y + dy);
        }
    }
}
